<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: pl125 <xskjs888@163.com>
// +----------------------------------------------------------------------
namespace api\portal\controller;

use api\common\controller\CommonController;
use api\portal\model\PortalPostModel;
use cmf\controller\RestBaseController;
use api\portal\model\PortalTagModel;
use cmf\lib\Storage;

class IndexController extends RestBaseController
{
    protected $tagModel;

    /**
     * 获取标签列表
     */
    public function index()
    {
        $this->success('请求成功!', "DD");
    }

    /**
     * 获取热门标签列表
     */
    public function hotTags()
    {
        $params                         = $this->request->get();
        $params['where']['recommended'] = 1;
        $data                           = $this->tagModel->getDatas($params);

        if (empty($this->apiVersion) || $this->apiVersion == '1.0.0') {
            $response = $data;
        } else {
            $response = ['list' => $data,];
        }
        $this->success('请求成功!', $response);
    }

    /**
     * 获取标签文章列表
     * @param int $id
     */
    public function articles($id)
    {
        if (intval($id) === 0) {
            $this->error('无效的标签id！');
        } else {
            $params    = $this->request->param();
            $postModel = new PortalPostModel();

            unset($params['id']);

            $articles = $postModel->paramsFilter($params)->alias('post')
                ->join('__PORTAL_TAG_POST__ tag_post', 'post.id = tag_post.post_id')
                ->where(['tag_post.tag_id' => $id])->select();

            if (!empty($params['relation'])) {
                $allowedRelations = $postModel->allowedRelations($params['relation']);
                if (!empty($allowedRelations)) {
                    if (count($articles) > 0) {
                        $articles->load($allowedRelations);
                        $articles->append($allowedRelations);
                    }
                }
            }


            $this->success('请求成功!', ['articles' => $articles]);
        }
    }

    /**
     * 文件上传
     */
    public function uploadFile()
    {
        $param = $this->request->param();
        if(empty($param['file_type'])){
            $this->error('请传入文件类型');
        }
        if(!$_FILES['file']['tmp_name']){
            $this->error('文件不能为空',$_FILES);
        }
        $dir = dirname(WEB_ROOT .'upload'. DIRECTORY_SEPARATOR . 'temp'. DIRECTORY_SEPARATOR . 'xx');
        if (!file_exists($dir)) {
            mkdir($dir, 0777, true);
        }
        $publicTempPath = PUBLIC_PATH . DIRECTORY_SEPARATOR . 'upload'. DIRECTORY_SEPARATOR . 'temp' .DIRECTORY_SEPARATOR . $_FILES['file']['name'];

        $allowExtension = ['image','video','audio','file'];
        if(!in_array($param['file_type'],$allowExtension)){
            $this->error('上传失败 类型不允许');
        }

        $uploadSetting = cmf_get_upload_setting();

        $arrFileTypes = [
            'image' => ['title' => 'Image files', 'extensions' => $uploadSetting['file_types']['image']['extensions']],
            'video' => ['title' => 'Video files', 'extensions' => $uploadSetting['file_types']['video']['extensions']],
            'audio' => ['title' => 'Audio files', 'extensions' => $uploadSetting['file_types']['audio']['extensions']],
            'file'  => ['title' => 'Custom files', 'extensions' => $uploadSetting['file_types']['file']['extensions']]
        ];

        $ext = cmf_get_file_extension($publicTempPath);
        if(!in_array($ext,explode(',',$arrFileTypes[$param['file_type']]['extensions']))){
            $this->error('上传失败 非法文件');
        }  
        if(move_uploaded_file($_FILES['file']['tmp_name'],$publicTempPath)){

            $storePath = 'portal/'.date('Ymd').'/'.time().mt_rand(11,88).'.'.cmf_get_file_extension($publicTempPath);
            $res = CommonController::uploadFile($storePath,$publicTempPath);
            
            if($res['preview_url']){
                unlink($publicTempPath);
                $this->success('上传成功',$res);

            }else{
                $this->error('上传失败');
            }
        }

    }
    /**
     * 图片上传
     */
    public function uploadImg()
    {
        $param = $this->request->param();
        if(!$_FILES['file']['tmp_name']){
            $this->error('文件不能为空',$_FILES);
        }
        $param['file_type']="image";
        $dir = dirname(WEB_ROOT .'upload'. DIRECTORY_SEPARATOR . 'temp'. DIRECTORY_SEPARATOR . 'xx');
        if (!file_exists($dir)) {
            mkdir($dir, 0777, true);
        }
        $publicTempPath = PUBLIC_PATH . DIRECTORY_SEPARATOR . 'upload'. DIRECTORY_SEPARATOR . 'temp' .DIRECTORY_SEPARATOR . $_FILES['file']['name'];

        $allowExtension = ['image','video','audio','file'];
        if(!in_array($param['file_type'],$allowExtension)){
            $this->error('上传失败 类型不允许');
        }

        $uploadSetting = cmf_get_upload_setting();

        $arrFileTypes = [
            'image' => ['title' => 'Image files', 'extensions' => $uploadSetting['file_types']['image']['extensions']],
            'video' => ['title' => 'Video files', 'extensions' => $uploadSetting['file_types']['video']['extensions']],
            'audio' => ['title' => 'Audio files', 'extensions' => $uploadSetting['file_types']['audio']['extensions']],
            'file'  => ['title' => 'Custom files', 'extensions' => $uploadSetting['file_types']['file']['extensions']]
        ];

        $ext = cmf_get_file_extension($publicTempPath);
        if(!in_array($ext,explode(',',$arrFileTypes[$param['file_type']]['extensions']))){
            $this->error('上传失败 非法文件');
        }  
        if(move_uploaded_file($_FILES['file']['tmp_name'],$publicTempPath)){

            $storePath = 'portal/'.date('Ymd').'/'.time().mt_rand(11,88).'.'.cmf_get_file_extension($publicTempPath);
            $res = CommonController::uploadFile($storePath,$publicTempPath);
            
            if($res['preview_url']){
                unlink($publicTempPath);
                $this->success('上传成功',$res);

            }else{
                $this->error('上传失败');
            }
        }

    }
}
